/**
 * Copyright (c) Css Team
 * All rights reserved.
 *
 * This file DelOrg.java 
 * http://www.css.com.cn
 */
package com.css.apps.base.org.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.OrgQueryProvider;
import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.org.model.SOrgPost;
import com.css.apps.base.sys.action.DelSys;
import com.css.core.configuration.Environment;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * <p>
 * 删除组织
 * </p>
 */
@SuppressWarnings("serial")
public class DelOrg extends AdminAction {
	
	private static Log log = LogFactory.getLog(DelSys.class);
	private String ids;

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(ids)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}

			List<String> listIds = StringHelper.strToList(ids);
			List<SOrg> orgList = QueryCache.idToObj(SOrg.class, listIds);
			int num = 0;
			tx = new TransactionCache();
			List<String> delOrgIds = new ArrayList<String>();
			Tree tree = new TreeCache(SOrg.class);
			for (SOrg org : orgList) {
				List<String> postIdlist = org.getPostList().getListById();
				List<String> userIdlist = org.getUserList().getListById();

				List<String> postIds = new ArrayList<String>();

				if ((userIdlist != null && userIdlist.size() > 0)
						|| (tree.getTreeNode(org.getUuid()).isLeaf() == false)) {
					num++;
				} else {
					if (postIdlist != null && postIdlist.size() > 0) {
						for (String postid : postIdlist) {
							postIds.add(postid.trim());
						}
						StringBuffer sb = new StringBuffer(
								"delete from SOrgPost a where a.orgId= :orgId and a.postId in (:listIds)");
						Map<String, Object> params = new HashMap<>();
						params.put("orgId", org.getUuid());
						params.put("listIds", postIds);
						tx.executeUpdate(sb.toString(), params);
						SOrg orgObj = QueryCache.get(SOrg.class, org.getUuid());
						orgObj.getPostList().removeAll();
					}
					org.setEditDate(new Date());
					org.setDelFlag("1");
					tx.update(org);
					LogPart lp = new LogPart();
					lp.setOpObjType(SOrg.class.getName());
					lp.setOpObjId(org.getUuid());
					lp.setRelObjType(SOrg.class.getName());
					lp.setRelObjId(org.getUuid());
					lp.setOpType(LogConstant.LOG_TYPE_DELETE);
					lp.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
					lp.setLogData("");
					lp.setResult(LogConstant.RESULT_SUCCESS);
					lp.save();
					delOrgIds.add(org.getUuid());
					List<TreeNode> treeNodes = tree.getTree();
					for (TreeNode node : treeNodes) {
						if (node.getNodeId().equals(org.getUuid())) {
							node.removeChild(node);
						}
					}
				}
			}
			tx.commit();

			for (String delorg : delOrgIds) {
				List<String> postIdlist = OrgQueryProvider.queryPostId(delorg);
				for (String postid : postIdlist) {
					LogPart lp = new LogPart();
					lp.setOpObjType(SOrgPost.class.getName());
					lp.setOpObjId(postid);
					lp.setRelObjType(SOrg.class.getName());
					lp.setRelObjId(delorg);
					lp.setOpType(LogConstant.LOG_TYPE_DELETE);
					lp.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
					lp.setLogData("");
					lp.setResult(LogConstant.RESULT_SUCCESS);
					lp.save();
				}
			}

			if (num > 0) {
				String str = Integer.toString(num);
				result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS,
						Messages.getString("systemMsg.NoDeleteOrgCount", new String[] { str }), delOrgIds);
				return Action.SUCCESS;
			}
			tree.refresh();
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"), listIds);
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			ex.printStackTrace();
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
